from flask import Flask, request, Response, jsonify, make_response

app = Flask(__name__)
jwt_secret = "default-jwt-secret"


@app.route('/')
def home():
    return "Hello, World!"


@app.route('/testPost', methods=['POST'])
def test_post():
    # 获取原始 JSON 数据
    json_data = request.get_json()
    x1 = json_data.get('x1')
    x2 = json_data.get('x2')

    return f'x1: {x1}, x2: {x2}'


@app.route('/updateJwtSecret', methods=['POST'])
def update_jwt_secret():
    # 获取新的 JWT secret
    new_jwt_secret = request.get_json().get('jwt_secret')
    global jwt_secret
    jwt_secret = new_jwt_secret

    return jsonify({"message": "success", "data": jwt_secret}), 200


@app.route('/getJwt', methods=['POST'])
def get_jwt():
    # 获取payload
    res_json = request.get_json()

    rsp = make_response()
    if not res_json:
        rsp.data = "No payload"
        rsp.status_code = 400
        return rsp
    else:
        rsp.data = res_json
        rsp.status_code = 200
        rsp.mimetype = "application/json"
        return rsp


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=True)
